package com.petadoption.serverclient.controller;

import com.petadoption.serverclient.service.IPetService;
import com.petadoption.serverclient.utils.Result;
import com.petadoption.serverclient.vo.PetVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 汪仕杰
 * @since 2023-08-28
 */
@Controller
@RequestMapping("/pet")
public class PetController {
    @Resource
    private IPetService petService;
    @Resource
    private Result result;

    @RequestMapping("/all")
    @ResponseBody
    public Result<PetVO> findPetAll() {
        List<PetVO> petList = petService.findPetAll();
        if (petList.isEmpty()) return result.fail("暂无数据");
        return result.success("宠物信息", petList);

    }

    @GetMapping("/search")
    @ResponseBody
    public Result findPet(String pet) {
        List<PetVO> petList = petService.findPet(pet);
        if (petList.isEmpty()) return result.fail("暂无数据");
        return result.success("查询结果", petList);
    }

    @GetMapping("/{id}")
    @ResponseBody
    public Result findPetById(@PathVariable int id) {
        System.out.println(id);
        PetVO petVO = petService.findPetById(id);
        if (petVO.getPetId() > 0) return result.success("查找成功", petVO);
        return result.fail("查询失败");
    }

    @DeleteMapping("/del/id")
    @ResponseBody
    public Result delById(Integer id) {
        int i = petService.delById(id);
        if (i > 0) return result.success("删除成功");
        return result.fail("删除失败");

    }


}
